package com.horay.statemachiedemo.multi.action;

import com.horay.statemachiedemo.multi.constant.StateMachineConstant;
import com.horay.statemachiedemo.multi.event.OrderEvent;
import com.horay.statemachiedemo.multi.model.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.Message;
import org.springframework.statemachine.annotation.OnTransition;
import org.springframework.statemachine.annotation.WithStateMachine;

/**
 * Class description
 *
 * @author heyingcheng
 * @date 2020/10/23
 */
@Slf4j
@WithStateMachine(id = StateMachineConstant.ORDER_MACHINE_ID)
public class OrderEventAction {

    /**
     * 当前状态UNPAID
     */
    @OnTransition(target = "UNPAID")
    public void create() {
        log.info("---订单创建，待支付---");
    }

    /**
     * UNPAID->WAITING_FOR_RECEIVE 执行的动作
     */
    @OnTransition(source = "UNPAID", target = "WAITING_FOR_RECEIVE")
    public void pay(Message<OrderEvent> message) {
        Order order = message.getHeaders().get(Order.class.getName(), Order.class);
        log.info("---用户完成支付，待收货---, order:[{}]", order);
    }

    /**
     * WAITING_FOR_RECEIVE->DONE 执行的动作
     */
    @OnTransition(source = "WAITING_FOR_RECEIVE", target = "DONE")
    public void receive(Message<OrderEvent> message) {
        Order order = message.getHeaders().get(Order.class.getName(), Order.class);
        log.info("---用户已收货，订单完成---, order:[{}]", order);
    }

}
